package com.doit.day03

import java.sql.{Connection, DriverManager, PreparedStatement}

/**
 * 算子  也就是方法
 * 只不过这个方法和其他的有点不一样，要传进去一个函数
 */
object _05_算子 {
  def main(args: Array[String]): Unit = {

    //需求：Array("hello,hello，hi，hello，hi"，"hadoop,hive,hadoop,hive,hadoop","biandan,chige,chige,biandan,chige")
    //Array("hello","hello","hi","hello","hi"，"hadoop","hive","hadoop","hive","hadoop","biandan","chige","chige","biandan","chige")；

    //flatten 压平 扁平化
                                    //字符数组
    val words: Array[Array[Char]] = Array(Array('b','i','a','d'),Array('c','h','i','g','e'))
    //有两层数组   我将里面的那一层数组里的所有元素都拿出来，给到最外面一层的数组
    val flatten: Array[Char] = words.flatten

//    Array(Array('b','i','a','d'),Array('c','h','i','g','e'))
//    Array('b','i','a','d','c','h','i','g','e')
    flatten.foreach((a:Char)=>{println(a)})


    val array: Array[Array[Int]] = Array(Array(1, 2, 3, 4, 5), Array(6, 7, 8), Array(1, 2, 3))
    val res5 :Array[Int]= array.flatten


    val array1: Array[Array[Any]] = Array(Array(1, 2, 3), Array("1", "2", "3"))
    array1.flatten.foreach(println)

    //他只能压一层
    val array2: Array[Array[String]] = Array(Array("hello", "hello", "hello"), Array("hello", "hello", "hello"))
//    val flatten1: Array[String] = array2.flatten
//    val flatten2: Array[Char] = flatten1.flatten










    //    filter
    val ints1: Array[Int] = Array(1, 2, 3, 4, 5)
    val res2: Array[Int] = ints1.filter((a: Int) => {
      a % 2 == 0
    })

    println(res2.mkString)//24


    //foreach    写的函数不能有返回值 一般用于打印
    ints1.foreach((a:Int)=>{println(a)})

  /*  val conn: Connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456")
    val pps: PreparedStatement = conn.prepareStatement("insert into guid values(?)")
    ints1.foreach(
      (a:Int)=>{
        pps.setInt(1,a)
        pps.execute()
      }
    )*/


    //map 用的非常的多
    val arr: Array[Int] = Array(1, 2, 3, 4, 5)

    val strings: Array[String] = arr.map((a: Int) => {
      a + ""
    })
    println(strings.mkString) //1, 2, 3, 4, 5

    val orders: Array[ScalaOrder] = arr.map((a: Int) => {
      ScalaOrder("001", "g001", 10, 20)
    })
    println(orders.mkString)


    val res1: Array[Unit] = arr.map((a: Int) => {
      println("nihaoe")
    })

    println(res1.mkString) //
    /**
     * 1, 2, 3, 4, 5
     * ScalaOrder("001", "g001", 10, 20), ScalaOrder("001", "g001", 10, 20), ScalaOrder("001", "g001", 10, 20), ScalaOrder("001", "g001", 10, 20), ScalaOrder("001", "g001", 10, 20)
     * nihaoe
     * nihaoe
     * nihaoe
     * nihaoe
     * nihaoe
     * (),(),(),(),()
     */


    //需求：想要将arr里面的每个元素都乘以10
    //创建一个长度和arr一样的数组，然后遍历原数组，将每一个元素都获取到，然后乘以10 ，在放入到新的数组中

    val res: Array[Int] = new Array[Int](arr.length)
    val res10: Array[Unit] = new Array[Unit](arr.length)

/*    for (elem <- 0 until arr.length) {
      println("nihao")
      res10(elem) = new Unit {}
    }*/

    println(res.mkString(","))

    //马大爷也提供给我们一个方法  map 做映射
    //传进去的类型，arr元素的类型是什么，传进去的类型就是什么
    //传出来呢？随便，你想是什么类型都可以

    val f = (a: Int) => {
      a * 10
    }
    val ints: Array[Int] = arr.map(f)
    println(ints.mkString(","))


  }
}
